<?php

namespace QyManage\Controller;

use QyManage\Controller\QyManageController;
use Think\Crypt\Driver\Xxtea;

/**
 * 企业管理-能力测评
 */
class AbilityCheckController extends QyManageController {

    /**
     * 能力评测列表
     */
    public function abilityList() {
        $iPageNo = $this->getParam('page_no', 1, 'int+');
        $strCateIds = $this->getParam('cate_ids', '', 'string');
        $strCheckLevels = $this->getParam('check_levels', '', 'string');
        $strRealName = $this->getParam('real_name', '', 'string');
        !empty($strCateIds) && $arrWhere['ac.cate_id'] = array('in', $strCateIds);
        !empty($strCheckLevels) && $arrWhere['ac.check_level'] = array('in', $strCheckLevels);

        $arrBind = array();
        if (!empty($strRealName)) {
            $arrWhere['u.real_name'] = array('like', ':real_name');
            $arrBind[':real_name'] = '%' . $strRealName . '%';
        }

        $arrWhere['ac.read_auth'] = array('exp', '& 2');
        $arrWhere['ac.company_id'] = $this->iCompanyId;
        $arrField = array(
            'ac.ability_check_id,ac.user_id,ac.check_level,ac.create_time',
            'c.company_name',
            'u.real_name, u.phone',
            'ca.cate_name',
            'ca.cate_id'
        );

        $arrJoin = array(
            'left join qy_company as c USING(company_id)',
            'left join qy_user as u ON u.user_id=ac.user_id',
            'left join qy_category_all as ca ON ca.cate_id = ac.cate_id AND ca.type = 1'
        );

        $objModel = M();
        $iCheckCount = $objModel->table('qy_ability_check ac')->field($arrField)->join($arrJoin)->where($arrWhere)->bind($arrBind)->count();
        $iPageSize = 10;
        $arrCheckList = $objModel->table('qy_ability_check ac')->field($arrField)->join($arrJoin)->where($arrWhere)->bind($arrBind)->order('ability_check_id DESC')->page($iPageNo)->limit($iPageSize)->select();
        if (!empty($arrCheckList)) {
            foreach ($arrCheckList as &$val) {
                $val['check_level_name'] = abilityLevelName($val['check_level']);
            }
        }
        $this->returnList($arrCheckList, $iPageNo, $iPageSize, $iCheckCount);
    }

    /**
     * 岗位和等级列表
     */
    public function postAndLevelList() {
        $cateList = M('CategoryAll')->where(array('type' => 1))->order('cate_id ASC')->select();
        $returnList['cate_list'] = $cateList;
        $returnList['level_list'] = abilityLevelName();
        ;
        $this->returnList($returnList);
    }

    /**
     * 能力评测删除(管理员)
     */
    public function abilityDeleteForAdmin() {
        $iAbilityCheckId = $this->getParam('ability_check_id', 0, 'int+', TRUE);
        $arrWhere['ability_check_id'] = $iAbilityCheckId;
        $arrWhere['read_auth'] = array('exp', '& 2');
        $arrWhere['company_id'] = $this->iCompanyId;
        $res = M('AbilityCheck')->where($arrWhere)->setField('read_auth', array('exp', 'read_auth - 2'));
        if ($res) {
            $this->success();
        } else {
            $this->error();
        }
    }

    /**
     * 能力测评各岗位数据
     */
    public function postStatisticsCount() {
        $arrWhere['read_auth'] = array('exp', '& 2');
        $arrWhere['company_id'] = $this->iCompanyId;
        $cateList = M('CategoryAll')->field('cate_id,cate_name')->where(array('type' => 1))->select();
        $statisticsData = M('AbilityCheck')->field('cate_id, count(DISTINCT user_id) as people_count')->where($arrWhere)->group('cate_id')->select();
        foreach ($cateList as &$val) {
            $val['people_count'] = 0;
            if (empty($statisticsData)) {
                continue;
            }
            foreach ($statisticsData as $vv) {
                if ($val['cate_id'] == $vv['cate_id']) {
                    $val['people_count'] = $vv['people_count'];
                }
            }
        }
        unset($val);

        $this->returnList($cateList);
    }

    /**
     * 生成测评链接
     * 获取测评链接token
     */
    public function createAbilityUrl() {
        $iCompanyId = $this->iCompanyId;
        $objXxtea = new Xxtea();
        $arrData['token'] = base64_encode($objXxtea->encrypt($iCompanyId, C('XXTEA_KEY')));
        $this->returnList($arrData);
    }

    /**
     * 个人能力报告信息
     */
    public function abilityReportInfo() {
        $iAbilityCheckId = $this->getParam('ability_check_id', '', 'int+', true);
        $iCompanyId = $this->iCompanyId;

        $AbilityCheckModel = new \Common\Model\AbilityCheckModel();
        $retrunList = $AbilityCheckModel->abilityCheckReportInfo($iAbilityCheckId, 2, $iCompanyId);

        $this->returnList($retrunList);
    }

}
